<?php

/**
 * 模板消息推送
 */

namespace Template;

use think\Db;

class Template
{

    //获取accesstoken

    public function getAccessToken($appid,$secret){
        $res = Db::name('wechat')->where(['wechat_id'=>2])->find();
        if($res['access_token'] && $res['expire_time'] > time())
        {
            //如果access_token在session中并没有过期
            return $res['access_token'];
        }else{
            //如果不存在或者已经过期,重新去取access_token
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";

            $res = $this->curl_get($url);
            $res = json_decode($res,1);
            //将重新取得access_token存入session
            $expire_time = time()+7000;
            Db::name('wechat')->where(['wechat_id'=>2])->update(['access_token'=>$res['access_token'],'expire_time'=>$expire_time]);
            return $res['access_token'];
        }

    }

    //获取模板消息内容主体

    //因为是测试所以写死，大家可以通过传参的方式获取

    public function getMsg($openid,$template_id,$form_id,$emphasis_keyword='keyword1'){

        $data['data']= ['keyword1'=>['value'=>'1001','color'=>'1'],'keyword2'=>['value'=>'汪洋','color'=>'2'],'keyword3'=>['value'=>'1W','color'=>'3'],'keyword4'=>['value'=>'水费','color'=>'4'],'keyword5'=>['value'=>'缴费成功','color'=>'5']];//内容主体

        $data['touser'] = $openid;//用户的openid

        $data['template_id'] = $template_id;//从微信后台获取的模板id

        $data['form_id'] = $form_id;//前端提供给后端的form_id

        $data['page'] = 'pages/index/index';//小程序跳转页面

        $data['emphasis_keyword'] = $emphasis_keyword;//选择放大的字体

        return $data;

    }

    public function send($appid,$secret,$openid,$template_id,$form_id){

        $access_token = $this->getAccessToken($appid,$secret);
        $send_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
        $data = $this->getMsg($openid,$template_id,$form_id);
        $str = $this->curl_post($send_url,json_encode($data));
        $str = json_decode($str,1);
        //returnJson(200,$str)->send();die;
        return $str;

    }

    public function curl_post($url, $fields, $data_type='text')

    {

        $cl = curl_init();

        if(stripos($url, 'https://') !== FALSE) {

            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($cl, CURLOPT_SSLVERSION, 1);

        }

        curl_setopt($cl, CURLOPT_URL, $url);

        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );

        curl_setopt($cl, CURLOPT_POST, true);

        curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);

        $content = curl_exec($cl);

        $status = curl_getinfo($cl);

        curl_close($cl);

        if (isset($status['http_code']) && $status['http_code'] == 200) {

            if ($data_type == 'json') {

                $content = json_decode($content);

            }

            return $content;

        } else {

            return FALSE;

        }

    }

    public function curl_get($url, $data_type='text')

    {

        $cl = curl_init();

        if(stripos($url, 'https://') !== FALSE) {

            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($cl, CURLOPT_SSLVERSION, 1);

        }

        curl_setopt($cl, CURLOPT_URL, $url);

        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );

        $content = curl_exec($cl);

        $status = curl_getinfo($cl);

        curl_close($cl);

        if (isset($status['http_code']) && $status['http_code'] == 200) {

            if ($data_type == 'json') {

                $content = json_decode($content);

            }

            return $content;

        } else {

            return FALSE;

        }

    }

}
